package zjut.raymond.collectorplus.view;

import java.util.HashMap;

public class SelectorGroup {
    public static final int MODE_SINGLE_CHOICE = 1;
    public static final int MODE_MULTIPLE_CHOICE = 2;

    private ChoiceAction choiceMode;
    private StateListener onStateChangeListener;
    /**
     * a map to keep previous selected selector
     */
    private HashMap<String, Selector> selectorMap = new HashMap<>();

    /**
     * customized an choice mode by yourself
     *
     * @param choiceMode
     */
    public void setChoiceMode(ChoiceAction choiceMode) {
        this.choiceMode = choiceMode;
    }

    /**
     * set a default choice mode
     *
     * @param mode
     */
    public void setChoiceMode(int mode) {
        switch (mode) {
            case MODE_MULTIPLE_CHOICE:
                choiceMode = new MultipleAction();
                break;
            case MODE_SINGLE_CHOICE:
                choiceMode = new SingleAction();
                break;
        }
    }

    public void setStateListener(StateListener onStateChangeListener) {
        this.onStateChangeListener = onStateChangeListener;
    }

    /**
     * get the selector which clicked last time by the specific group tag
     *
     * @param groupTag a tag which the previous selector belongs to
     * @return
     */
    public Selector getPreSelector(String groupTag) {
        return selectorMap.get(groupTag);
    }

    /**
     * toggle or cancel one choice
     *
     * @param selected
     * @param selector
     */
    public void setSelected(boolean selected, Selector selector) {
        if (selector == null) {
            return;
        }
        if (selected) {
            //keep click selector in map
            selectorMap.put(selector.getGroupTag(), selector);
        }
        selector.setSelected(selected);
        if (onStateChangeListener != null) {
            onStateChangeListener.onStateChange(selector.getGroupTag(), selector.getSelectorTag(), selected);
        }
    }

    /**
     * cancel selected state of one Selector when another is selected
     *
     * @param selector the Selector which is selected right now
     */
    private void cancelPreSelector(Selector selector) {
        String groupTag = selector.getGroupTag();
        Selector preSelector = getPreSelector(groupTag);
        if (preSelector != null) {
            preSelector.setSelected(false);
        }
    }

    /**
     * add extra layer which means more complex
     *
     * @param selector
     */
    void onSelectorClick(Selector selector) {
        if (choiceMode != null) {
            choiceMode.onChoose(selector, this, onStateChangeListener);
        }
        //keep click selector in map
        selectorMap.put(selector.getGroupTag(), selector);
    }

    public void clear() {
        if (selectorMap != null) {
            selectorMap.clear();
        }
    }

    public interface ChoiceAction {
        /**
         * invoked when one selector is clicked
         *
         * @param selector      the clicked selector
         * @param selectorGroup
         * @param stateListener
         */
        void onChoose(Selector selector, SelectorGroup selectorGroup, StateListener stateListener);
    }

    /**
     * pre-defined choice mode: previous choice will be canceled if there is a new choice
     */
    private class SingleAction implements ChoiceAction {

        @Override
        public void onChoose(Selector selector, SelectorGroup selectorGroup, StateListener stateListener) {
            cancelPreSelector(selector);
            setSelected(true, selector);
        }
    }

    /**
     * pre-defined choice mode: all choices will be preserved
     */
    private class MultipleAction implements ChoiceAction {

        @Override
        public void onChoose(Selector selector, SelectorGroup selectorGroup, StateListener stateListener) {
            boolean isSelected = selector.isSelected();
            setSelected(!isSelected, selector);
        }
    }

    public interface StateListener {
        void onStateChange(String groupTag, String tag, boolean isSelected);
    }


//    private HashMap<String, Selector> selectorMap = new HashMap<>();
//
//    public Selector getPreSelector(String groupTag) {
//        return selectorMap.get(groupTag);
//    }
//
//    private void cancelPreSelector(Selector selector) {
//        String groupTag = selector.getGroupTag();
//        Selector preSelector = getPreSelector(groupTag);
//        if(preSelector != null) {
//            preSelector.setSelected(false);
//        }
//    }
//
//    public void onSelectorClick(Selector selector) {
//        selector.setSelected(true);
//        cancelPreSelector(selector);
//        selectorMap.put(selector.getGroupTag(), selector);
//    }
}
